<template>
  <div class="withdraw-audit-div">
    <search-table
      :url="url"
      ref="searchtable"
      :hideSelect="true"
      :columns="columns"
      :defaultParams="defaultParams"
      :headParams="headParams"
      :form="form">
      <!-- 搜索条件 -->
      <template slot="searchForm">
        <a-form layout="inline" :form="form">
          <a-form-item label="账单时间">
            <fast-range-picker
              style="width: 240px"
              v-decorator="['startDate-endDate']">
            </fast-range-picker>
          </a-form-item>
        </a-form>
      </template>
      <!-- 操作栏 -->
      <template slot="oprationRow">
        <permission authority="withdraw:auditor:export">
          <a-button type="primary" @click="exportExcel()">导出</a-button>
        </permission>
      </template>
      <!-- 表格行操作 -->
      <template slot="tableOperation" slot-scope="{ record }">
        <a-space>
          <permission authority="withdraw:audit:info">
            <span class="text-primary pointer" @click="openInfo(record, 'view')">详情</span>
          </permission>
          <permission v-if="record.auditorStatus == 1 || record.auditorStatus == 3" authority="withdraw:auditor:modify">
            <span class="text-primary pointer" @click="openInfo(record, 'edit')">审核</span>
          </permission>
          <permission v-if="record.auditorStatus == 2" authority="shop:bill:withdrawal">
            <span class="text-primary pointer" @click="withdrawMoney(record)">提现</span>
          </permission>
        </a-space>
      </template>
    </search-table>
    <!-- 弹窗 -->
    <auditModal ref="auditModal" :handle="handle" :record="currentRecord" :visible="infoVisible" @cancel="closeModal"></auditModal>
  </div>
</template>
<script>
import { findDictNameByValue,createOptionsByDict } from '@/utils/dict'
import auditModal from './components/detail';

export default {
  name: 'withdrawAuditTable',
  components: {
    auditModal
  },
  data() {
    return {
      url: '/withdraw/auditor/page', // 列表接口地址
      headParams: {}, // 请求头参数
      defaultParams: {}, // 默认携带参数
      currentRecord: {}, // 选中行数据
      infoVisible: false, // 弹窗是否显示
      handle: '', // 操作类型
      form: this.$form.createForm(this, { name: "table_search" }), // 搜索表单
      columns: [
        {
          title: "时间",
          dataIndex: "createDate",
          key: "createDate",
        },
        {
          title: "供应商名称",
          dataIndex: "merchantName",
          key: "merchantName",
        },
        {
          title: "类型",
          dataIndex: "type",
          key: "type",
          customRender: (text, record) => '提现' // findDictNameByValue('DEAL_TYPE', text)
        },
        {
          title: "交易名称",
          dataIndex: "transactionName",
          key: "transactionName",
        },
        {
          title: "提现订单号",
          dataIndex: "withdrawOrderNo",
          key: "withdrawOrderNo",
        },
        {
          title: "收款账户",
          dataIndex: "receiverAccount",
          key: "receiverAccount",
        },
        {
          title: "金额",
          dataIndex: "money",
          key: "money",
          customRender:(text,row)=>{
            return text ? '¥ '+(Number(text)/100).toFixed(2) : '¥ 0.00'
          }
        },
        {
          title: "状态",
          dataIndex: "auditorStatus",
          key: "auditorStatus",
          customRender: (text, record) => findDictNameByValue('WITHDRAW_AUDIT_STATUS', text)
        },
        {
          title: "操作",
          dataIndex: "operation",
          scopedSlots: { customRender: "operation" },
        }
      ]
    };
  },
  created() {},
  mounted() {},
  methods: {
    // 打开详情
    openInfo(record, type) {
      this.currentRecord = record
      this.$refs.auditModal.defaultParams = {
        auditorStatus: record.auditorStatus == 4 ? 2 : record.auditorStatus,
        withdrawOrderNo: record.withdrawOrderNo
      }
      this.infoVisible = true
      this.handle = type || 'edit'
    },
    // 提现
    withdrawMoney(record) {
      // 
      this.$confirm({
        title: '提示',
        content: '请确认是否提现？',
        okText: '确认',
        cancelText: '取消',
        onOk: () => {
          this.$post('/shop/bill/withdrawal',
            {
              withdrawOrderNo: record.withdrawOrderNo,
              withdrawalPrice: record.money
            })
          .then(res => {
            if (res && res.returnCode === 1000) {
              this.$message.success('提现成功，预计1-3个工作日到账。')
              this.$refs.searchtable.refreshTable()
            }
          })
          .catch(err => {
            // this.$message.error('提现失败！')
          })
        }
      });
    },
    // 关闭弹窗
    closeModal(freshTable) {
      this.infoVisible = false
      if (freshTable) {
        this.$refs.searchtable.refreshTable();
      }
    },
    // 导出
    exportExcel() {
      this.$refs.searchtable.getFormValues().then((values) => {
        this.$refs.searchtable.searchParams = {
          ...this.defaultParams,
          ...values
        }

        // if(!this.$refs.searchtable.searchParams.startDate || !this.$refs.searchtable.searchParams.endDate){
        //   this.$message.warning('请选择导出的开始时间和结束时间！');
        //   return false
        // }
        
        this.$refs.searchtable.getList()

        // if (this.$refs.searchtable.dataSource.length == 0) {
        //   this.$message.warning('页面数据为空无法导出！');
        //   return false
        // }

        this.$export('/withdraw/auditor/export', this.$refs.searchtable.searchParams, false)
      })
    }
  }
};
</script>

<style lang="less" scoped></style>
